package com.cictec.middleware.tde.rm.protocol.rm.encode;

import java.nio.ByteBuffer;

import com.cictec.middleware.commons.utils.BinaryUtils;
import com.cictec.middleware.commons.utils.gps.GpsUtils;
import com.cictec.middleware.tde.exception.MessageEncoderException;
import com.cictec.middleware.tde.rm.protocol.AbstractMessageEncoder;
import com.cictec.middleware.tde.rm.protocol.TerminalMessage;
import com.cictec.middleware.tde.rm.protocol.annotion.TerminalMessageEncoder;
import com.cictec.middleware.tde.rm.protocol.rm.message.Rm0x03TerminalMessage;
import com.cictec.middleware.tde.rm.protocol.rm.utils.RmBinaryUtils;

/**
 * 0x02 上传 GPS 定位数据
 *
 */
@TerminalMessageEncoder(0x03)
public class Rm0x03Encoder extends AbstractMessageEncoder {

	@Override
	public byte[] encode(TerminalMessage message) throws MessageEncoderException {
		Rm0x03TerminalMessage msg = (Rm0x03TerminalMessage) message;

		ByteBuffer out = ByteBuffer.allocate(256);

		out.put(RmBinaryUtils.dateToDwordRM(msg.getCreateTime()));
		RmBinaryUtils.setLatOrLngByteDataRM(out, GpsUtils.formatLongitude(String.valueOf(msg.getLat()), "dm"));
		RmBinaryUtils.setLatOrLngByteDataRM(out, GpsUtils.formatLongitude(String.valueOf(msg.getLng()), "dm"));

		out.put(BinaryUtils.intToUnsignedByte(msg.getSpeed()));
		out.put(BinaryUtils.intToUnsignedByte(msg.getAngle()));
		out.put(BinaryUtils.intToWord(msg.getAltitude()));
		out.put(BinaryUtils.intToUnsignedByte(msg.getPositionType()));
		out.put(BinaryUtils.intToUnsignedByte(msg.getPositionNumber()));
		out.put(BinaryUtils.intToUnsignedByte(msg.getEnterOutStatus()));
		out.put(BinaryUtils.intToUnsignedByte(msg.getTriggerType()));
		out.put(BinaryUtils.intToUnsignedByte(msg.getGetOnNumber()));
		out.put(BinaryUtils.intToUnsignedByte(msg.getGetOffNumber()));
		out.put(BinaryUtils.intToWord(msg.getReserved()));

		return toBytes(out);
	}

}
